package com.hllxd.hism.star.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class CommonUtils {

    static DecimalFormat df = new DecimalFormat(".00");
    public static String doubleFormat_2(double d) {

        return df.format(d);
    }

    public static double round_2(double d) {
        BigDecimal b = new BigDecimal(d);
        return b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double round(double d, int scale) {
        BigDecimal b = new BigDecimal(d);
        return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 获取当前日期，默认格式为"yyyy-MM-dd HH:mm:ss"
     * */
    public static String getDateByDefault() {
        return getDateByFormat("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取当前日期，按照所传格式
     * */
    public static String getDateByFormat(String format) {
        Date date=new Date();
        DateFormat df =new SimpleDateFormat(format);
        String time= df.format(date);
        return time;
    }


//    public static List<String> trimAndSplitBySemicolon(String s) {
//        return (ArrayList) Arrays.asList(s.trim().split("\\s*;\\s*"));
//    }

    private static final Charset CHARSET = Charset.forName("ISO-8859-1");
    public static byte[] toBytes(final String s) {
        return s.getBytes(CHARSET);
    }
    public static String fromBytes(final byte[] b) {
        return new String(b, CHARSET);
    }

    public static String time(String format,long ms) {

        Date date = new Date(ms);
        SimpleDateFormat df = new SimpleDateFormat(format);
        df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return df.format(date);
    }

    public static BigDecimal objectConvertBigDecimal(Object value) {
        BigDecimal ret = null;
        if (value != null) {
            if (value instanceof BigDecimal) {
                ret = (BigDecimal) value;
            } else if (value instanceof String) {
                ret = new BigDecimal((String) value);
            } else if (value instanceof BigInteger) {
                ret = new BigDecimal((BigInteger) value);
            } else if (value instanceof Number) {
                ret = new BigDecimal(((Number) value).doubleValue());
            } else {
                throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
                        + " into a BigDecimal.");
            }
        }
        return ret;
    }

    public static <T> boolean isNull(T... objects) {
        for (T obj : objects) {
            if (null == obj) {
                return true;
            }
        }
        return false;
    }


    public static void main(String[] args) {

        String uri = "/hllxd/api/basic/v1/user/userInfo.do";
        String[] items = uri.split("/");
        for(int i=0;i<items.length;++i) {
            System.out.println(items[i]);
        }
        System.out.println(items[3]);
        StringBuilder sb = new StringBuilder();
        for(int i=4; i<items.length; ++i) {
            sb.append("/").append(items[i]);
        }
        System.out.println(sb.toString());

    }
}
